/*
 * v1.1 by jetsum tonydon
 * netbeansIDE 7.1
 */
package cn.txdnet.txweb;

import cn.txdnet.txweb.utils.JsonUtil;
import cn.txdnet.txweb.utils.VelocityUtil;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import txdnet.util.StaticMark;

/**
 *
 * @author txdnet
 */
public class Action{
    
    protected static void write(HttpServletResponse res, Object obj) throws Exception{
         res.getWriter().print(obj);
    }
    
    protected static void forward(HttpServletRequest req, HttpServletResponse res, String page) throws Exception{
        req.getRequestDispatcher(page).forward(req, res);
    }
    
    protected static void redirect(HttpServletResponse res, String url) throws Exception{
        res.sendRedirect(url);
    }
    
    protected static void outTextPage(HttpServletRequest req, HttpServletResponse res, Object obj) throws Exception {
        req.setAttribute("text", obj);
        req.getRequestDispatcher("/META-INF/out_text.jsp").forward(req, res);
    }
    
    protected static void outJsonPage(HttpServletRequest req, HttpServletResponse res, Object obj) throws Exception {
        req.setAttribute("json", JsonUtil.toJSONString(obj));
        req.getRequestDispatcher("/META-INF/out_json.jsp").forward(req, res);
    }
    
    protected static void outLowerCaseJsonPage(HttpServletRequest req, HttpServletResponse res, Object obj) throws Exception {
        req.setAttribute("json", JsonUtil.toLowerKeyJSONString(obj));
        req.getRequestDispatcher("/META-INF/out_json.jsp").forward(req, res);
    }
    
    protected static void outViewPage(HttpServletResponse res, String vm_file, Map m) throws IOException{
        res.getWriter().print(
                VelocityUtil.mergeTemplateToString(
                    vm_file.concat(VelocityUtil.VM_EXT), m, StaticMark.ENCODING_UTF8));
    }
}